home *** CD-ROM | disk | FTP | other *** search
- Path: nntp1.best.com!usenet
- From: javaprog@best.com (John Lockwood)
- Newsgroups: comp.lang.c++
- Subject: Re: Calling the wrong constructor
- Date: Sun, 14 Apr 1996 01:58:19 GMT
- Organization: Best Internet Communications
- Message-ID: <4kpm4c$jfe@nntp1.best.com>
- References: <4kot87$796@earth.njcc.com>
- NNTP-Posting-Host: javaprog.vip.best.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- mike@pluto.njcc.com (Michael Hohenshilt) wrote:
-
- > This might seem like a basic question, but lets say you something set u
- >like the following:
-
- >class Parent { Parent() { allocsomething} ~Parent() { deletesomething }... };
- >class Foo : Parent { Foo() { allocsomething } ~Foo() {deletesomething }... };
- >class Contain { Parent *ptr; ... };
-
- >both parent, and foo allocate memory, and will free it up when being
- >destructed. Contain just holds a pointer of type Parent, and its
- >constructor takes such a pointer as a parameter.
- > If I store a pointer of type Foo into contain.ptr and discard that
- >pointer. Is there any way to have contain (via destructors) free up all
- >memory allocated by Foo and/or Parent?
- > Mike
-
- The destructors need to be virtual.
-
- virtual ~Foo() { { /* ... */ }
- virtual ~Parent() { /* ... */ }
-
- Then a base class pointer pointing to a derived class will do the
- right thing. Note that the code as you've written it wouldn't
- do any base class allocation, since you didn't call your parent
- constructor.
-
- Bad programmer! Just for that, no JOLT cola! ;-)
-
-
-
-
-
-
-
- Regards,
-
-
-
- John Lockwood
- john@wwg.com
- javaprog@best.com
- http://www.best.com/~javaprog
-
-